package interview.dfs;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * 2023.04.12-实习笔试-第二题-获取最多食物 https://blog.codefun2000.com/company/hw/23-4-12/P1190.html
 */
public class GetMaxFood {

    static List<Integer>[] childList;
    static int[] gridValue;
    static long maxResult = Long.MIN_VALUE;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        gridValue = new int[n];
        childList = new ArrayList[n];
        for (int i = 0;i < n;i++)
            childList[i] = new ArrayList<>();
        int root = 0;
        for (int i = 0;i < n;i++) {
            int id = sc.nextInt();
            int parentId = sc.nextInt();
            int value = sc.nextInt();
            gridValue[id] = value;
            if (parentId != -1) childList[parentId].add(id);
            else root = id;
        }
        dfs(root);
        System.out.println(maxResult);
    }

    /**
     * 返回从root节点出发的最大路径价值和
     */
    public static long dfs(int root) {
        long maxChild = 0;//考虑子路径都为负数的情况
        for (Integer child : childList[root]) {
            maxChild = Math.max(maxChild, dfs(child));
        }
        long maxRootPath = gridValue[root] + maxChild;
        maxResult = Math.max(maxResult, maxRootPath);//更新结果
        return maxRootPath;
    }
}
